package com.yun.platform.es.service;

import com.yun.platform.es.entity.Demo;
import com.yun.platform.es.esdao.DemoRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 高版本 spring-es-plus demoRepository操作实体
 * ElasticsearchRestTemplate 操作查询等，这个接口更丰富的操作。
 */
@Service
public class DemoService {

    @Autowired
    DemoRepository demoRepository;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    public int saveProduct(Demo en) {
        Demo result = demoRepository.save(en);
        return result.getId();
    }

    public List<Demo> getAll() {
        Iterable<Demo> products = demoRepository.findAll();
        List<Demo> productList = new ArrayList<>();
        for (Demo product : products) {
            productList.add(product);
        }
        return productList;
    }

    public List<Demo> searchProduct(Integer pageNum, Integer pageSize, String searchName,String pid) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");//new Sort(Sort.Direction.DESC, Arrays.asList("id"))
        Pageable pageable = PageRequest.of(pageNum, pageSize, sort);
        //matchPhraseQuery和matchQuery等的区别，在使用matchQuery等时，在执行查询时，搜索的词会被分词器分词，而使用matchPhraseQuery时，不会被分词器分词，而是直接以一个短语的形式查询，
        QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("name", searchName);
        QueryBuilder queryBuilder2 = QueryBuilders.termsQuery("pid", pid);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().should(queryBuilder).must(queryBuilder2);

        Query searchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(boolQueryBuilder).build();
        Demo d = new Demo();
        d.setPid(pid);
        d.setName(searchName);
        Page<Demo> products = demoRepository.searchSimilar(d,new String[]{"name","pid"},pageable);

        return products.getContent();
    }

    public List<Demo> searchProduct2(Integer pageNum, Integer pageSize, String searchName,String pid) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");//new Sort(Sort.Direction.DESC, Arrays.asList("id"))
        Pageable pageable = PageRequest.of(pageNum, pageSize, sort);
        //matchPhraseQuery和matchQuery等的区别，在使用matchQuery等时，在执行查询时，搜索的词会被分词器分词，而使用matchPhraseQuery时，不会被分词器分词，而是直接以一个短语的形式查询，
        QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("name", searchName);
        QueryBuilder queryBuilder2 = QueryBuilders.termsQuery("pid", pid);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().should(queryBuilder).must(queryBuilder2);

        Query searchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(boolQueryBuilder).build();

        SearchHits<Demo> searchHits = elasticsearchRestTemplate.search(searchQuery, Demo.class);


        return null;
    }

}
